package cn.yogehaoren.agile_gateway.config;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author WangNing
 * @version 1.0
 * @date 2020/11/22 21:55
 */
//@Component
public class FileRouteDefinitionRepository implements RouteDefinitionRepository, ApplicationEventPublisherAware {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileRouteDefinitionRepository.class);
    private ApplicationEventPublisher publisher;
    private List<RouteDefinition> routeDefinitionList = new ArrayList<>();



    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }


    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {

        return Mono.defer(() -> Mono.error(new NotFoundException("Unsupported operation")));

    }

    @Override
    public Mono<Void> delete(Mono<String> routeId) {

        return Mono.defer(() -> Mono.error(new NotFoundException("Unsupported operation")));
    }

    @SneakyThrows
    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.setId("Test");
        routeDefinition.setUri(new URI("http://www.baidu.com"));
        PredicateDefinition predicate = new PredicateDefinition("Path=/bpi/datareduce/**");
        routeDefinition.setPredicates(Collections.singletonList(predicate));
        return Flux.just(routeDefinition);
    }
}